//
//  FindPassViewController.m
//  Vpay
//
//  Created by 伟大大 on 2018/9/15.
//  Copyright © 2018年 丁伟佳. All rights reserved.
//

#import "FindPassViewController.h"
#import "WKVerCodeImageView.h"
@interface FindPassViewController () {
    NSString *str;

}
@property (nonatomic, strong) WKVerCodeImageView *codeImageView;

@end

@implementation FindPassViewController

- (void)viewWillAppear:(BOOL)animated {
    
    [super viewWillAppear:animated];
    //    rightNavBtn.hidden = NO;
    
    //    self.navigationController.navigationBar.barTintColor = RGBCOLOR(255, 255, 255);
    [theApp hiddenBottomView];
    
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    //    [theApp showBottomView];
    //    rightNavBtn.hidden = YES;
}

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    //    self.navigationController.navigationBar.barTintColor = RGBCOLOR(255, 255, 255);
    
    
}

- (void)viewDidDisappear:(BOOL)animated {
    [super viewDidDisappear:animated];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view = [[[NSBundle mainBundle]loadNibNamed:@"FindPassViewController" owner:self options:nil]lastObject];
    self.title = @"找回登录密码";
    [self.navigationController.navigationBar setTitleTextAttributes:
     
     @{NSFontAttributeName:[UIFont systemFontOfSize:17],
       
       NSForegroundColorAttributeName:[UIColor whiteColor]}];
    [self initNavBtns];
    [self initViews];
    
    
    // Do any additional setup after loading the view from its nib.
}
#pragma mark -
#pragma mark - for Views
#pragma mark -
- (void)initNavBtns {
    
    self.navigationItem.leftBarButtonItem = [AppUtility getNavBtnItemLefBacktWith:@selector(leftNavBtnClicked) andTarget:self];
}
- (void)initViews {
    
    self.phone_textfiled.placeholder = NSLocalizedString(@"fphoneno", nil);
    self.YZM_textfiled.placeholder = NSLocalizedString(@"fcode", nil);
    self.NewPass_textfiled.placeholder = NSLocalizedString(@"fnewpass", nil);
    [self.Confirm_btn setTitle:NSLocalizedString(@"fconfirm", nil) forState:UIControlStateNormal];
    
    
    [self.YZM_textfiled addTarget:self action:@selector(textFiledDidChange:) forControlEvents:UIControlEventAllEvents];

    if (([[[NSUserDefaults standardUserDefaults] objectForKey:@"myLanguage"] isEqualToString:@"zh-Hans"] && ![[[NSUserDefaults standardUserDefaults] objectForKey:@"myLanguage"] isEqualToString:@""])||![[NSUserDefaults standardUserDefaults] objectForKey:@"myLanguage"]) {
        
    }else {
        self.labe1.hidden = YES;
        self.label2.hidden = YES;
        self.label3.hidden = YES;
        
        [self.phone_textfiled setFrame:CGRectMake(10*theApp.autoSizeScaleX, self.phone_textfiled.frame.origin.y, self.phone_textfiled.frame.size.width, self.phone_textfiled.frame.size.height)];
        [self.YZM_textfiled setFrame:CGRectMake(10*theApp.autoSizeScaleX, self.YZM_textfiled.frame.origin.y, 300*theApp.autoSizeScaleY, self.YZM_textfiled.frame.size.height)];
        [self.NewPass_textfiled setFrame:CGRectMake(10*theApp.autoSizeScaleX, self.NewPass_textfiled.frame.origin.y, self.NewPass_textfiled.frame.size.width, self.NewPass_textfiled.frame.size.height)];

    }
    
    UITapGestureRecognizer *tap1 = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapTouched)];
    //    tap.delegate = self;
    [self.view addGestureRecognizer:tap1];
    
    
    if (isTimer!=0)
    {
        
        [self.HQYZM_btn setTitle:[NSString stringWithFormat:@"%d%@",isTimer,NSLocalizedString(@"freget", nil)] forState:UIControlStateNormal];
        self.HQYZM_btn.userInteractionEnabled = NO;
        
        [countDownTimer invalidate];
        countDownTimer = nil;
        countDownTimer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(timerFired) userInfo:nil repeats:YES];
        
        
        
    }
    else
    {
        [self.HQYZM_btn setTitle:NSLocalizedString(@"fgetcode", nil) forState:UIControlStateNormal];
    }
    
    [self.HQYZM_btn.titleLabel setFont:[UIFont systemFontOfSize:13]];
    //        [_yanZhengBtn setBackgroundColor:TITLECOLOR];
    [self.HQYZM_btn setTitleColor:RGBCOLOR(255, 0, 0) forState:UIControlStateNormal];
    
    _codeImageView = [[WKVerCodeImageView alloc] initWithFrame:CGRectMake([AppUtility screenWidth]-10*theApp.autoSizeScaleX-70*theApp.autoSizeScaleX, 110*theApp.autoSizeScaleY, 70*theApp.autoSizeScaleX, 25*theApp.autoSizeScaleY)];
    _codeImageView.hidden = YES;
    _codeImageView.bolck = ^(NSString *imageCodeStr){//看情况是否需要
        NSLog(@"imageCodeStr = %@",imageCodeStr);
        str=[NSString stringWithFormat:@"%@", imageCodeStr];
    };
    //是否需要斜体
    _codeImageView.isRotation = YES;
    [_codeImageView freshVerCode];
    
    //点击刷新
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapClick:)];
    [_codeImageView addGestureRecognizer:tap];
    [self.TopView addSubview: _codeImageView];
    
}
#pragma mark -
#pragma mark for evevts
#pragma mark -
- (void)leftNavBtnClicked {
    
    [self.navigationController popViewControllerAnimated:YES];
    //    [self.navigationController popToRootViewControllerAnimated:YES];
}
- (void)tapClick:(UITapGestureRecognizer*)tap
{
    [_codeImageView freshVerCode];
    
}
- (void)textFiledDidChange:(UITextField *)textfiled {
    if (self.YZM_textfiled.text.length > 6) {
        //提示语
        //        [self showMessage:@"输入限制20个字符以内"];
        //截取
        self.YZM_textfiled.text = [textfiled.text substringToIndex:6];
    }
}
-(void)timerFired
{
    if (isTimer!=0) {
        
        isTimer--;
        [self.HQYZM_btn setTitle:[NSString stringWithFormat:@"%d%@",isTimer,NSLocalizedString(@"freget", nil)] forState:UIControlStateNormal];
        
    }
    else
    {
        [countDownTimer invalidate];
        [self.HQYZM_btn setTitle:NSLocalizedString(@"fgetcode", nil) forState:UIControlStateNormal];
        self.HQYZM_btn.userInteractionEnabled = YES;
    }
}
-(void)tapTouched{
    
    if ([_phone_textfiled isFirstResponder]) {
        [_phone_textfiled resignFirstResponder];
    }
    if ([_YZM_textfiled isFirstResponder]) {
        [_YZM_textfiled resignFirstResponder];
    }
    if ([_NewPass_textfiled isFirstResponder]) {
        [_NewPass_textfiled resignFirstResponder];
    }
    if ([_TX_textfiled isFirstResponder]) {
        [_TX_textfiled resignFirstResponder];
    }
   
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

- (void)dealloc {
    [_phone_textfiled release];
    [_YZM_textfiled release];
    [_NewPass_textfiled release];
    [_HQYZM_btn release];
    [_labe1 release];
    [_label2 release];
    [_label3 release];
    [_Confirm_btn release];
    [_YZM_textfiled release];
    [_TopView release];
    [_lastspliteView release];
    [_TX_label release];
    [super dealloc];
}
- (IBAction)Confirm_btnClik:(UIButton *)sender {
    

    if ([AppUtility isBlankString:_phone_textfiled.text]) {
        [self.view makeToast:NSLocalizedString(@"findphonets", nil)];
        return;
    }
    if ([AppUtility isBlankString:_YZM_textfiled.text]) {
        [self.view makeToast:NSLocalizedString(@"findyzm", nil)];
        return;
    }
    if (![self inputShouldNumber:_YZM_textfiled.text]||self.YZM_textfiled.text.length<6) {
        [self.view makeToast:NSLocalizedString(@"findyzmjudege", nil)];
        return;
    }
    
    if ([AppUtility isBlankString:_NewPass_textfiled.text]) {
        [self.view makeToast:NSLocalizedString(@"findnewpass", nil)];
        return;
    }
    if (![AppUtility judgePassWordLegal:_NewPass_textfiled.text]) {
        [self.view makeToast:NSLocalizedString(@"delmmzmts", nil)];
        return;
    }
    
    BOOL result1 =  [_TX_textfiled.text compare:str
                                         options:NSCaseInsensitiveSearch | NSNumericSearch]==NSOrderedSame;
    
    if (result1) {
        NSLog(@"succes");
    }else{
        [self.view makeToast:@"验证码输入错误"];
        return;
    }
    
//    if (![AppUtility checkTel:self.phone_textfiled.text]) {
//
//        return;
//    }
    
        NSString *postStr = [NSString stringWithFormat:@"&mobile=%@&code=%@&password=%@",_phone_textfiled.text,_YZM_textfiled.text,_NewPass_textfiled.text];
        NSDictionary *resultDict =[NetUtility getDataWithURLStr:KURLForgetPass andPostStr:postStr andView:self.navigationController.view];
    
        UEXNSLog(@"kURL==%@%@ resultDict=%@",KURLForgetPass,postStr,resultDict);
    
        if (resultDict) {
            int stauts = [[resultDict objectForKey:@"s"] intValue];
    
            if (stauts==1) {
    
                //            NSMutableDictionary *dic =[NSMutableDictionary dictionaryWithDictionary: [AccountController getAccountInfo]];
                //
                //            [dic setObject:textField.text forKey:@"nickname"];
                //            [AccountController setAccountInfo:dic];
    
                [self.navigationController popViewControllerAnimated:YES];
            }
    
                [self.navigationController.view makeToast:[resultDict objectForKey:@"i"]];
        }
}
- (IBAction)HQYZM_btnClik:(UIButton *)sender {
    [self.HQYZM_btn resignFirstResponder];
    
    if ([AppUtility isBlankString:_phone_textfiled.text]) {
        [self.view makeToast:NSLocalizedString(@"findphonets", nil)];
        return;
    }
    
    self.TX_label.hidden = NO;
    self.TX_textfiled.hidden = NO;
    _codeImageView.hidden = NO;
    _lastspliteView.hidden = NO;
//    if (![AppUtility checkTel:self.phone_textfiled.text]) {
//
//        return;
//    }
    if ([AppUtility isBlankString:_TX_textfiled.text]) {
        [self.view makeToast:@"请填写图形验证码"];
        return;
    }
    BOOL result1 =  [_TX_textfiled.text compare:str
                                        options:NSCaseInsensitiveSearch | NSNumericSearch]==NSOrderedSame;
    
    if (result1) {
        NSLog(@"succes");
    }else{
        [self.view makeToast:@"验证码输入错误"];
        return;
    }
    NSString *postStr = [NSString stringWithFormat:@"&mobile=%@",_phone_textfiled.text];

    NSDictionary *resultDict = [NetUtility getDataWithURLStr:KURLSendSMS andPostStr:postStr];
    
    UEXNSLog(@"URL====%@==== resultDict=%@",[NSString stringWithFormat:@"%@mobile=%@",KURLSendSMS,_phone_textfiled.text],resultDict);
    
    if ([[resultDict objectForKey:@"s"]intValue] == 1) {
        
        [self.HQYZM_btn setTitle:[NSString stringWithFormat:@"60%@",NSLocalizedString(@"freget", nil)] forState:UIControlStateNormal];
        isTimer = 60;
        self.HQYZM_btn.userInteractionEnabled = NO;
        
        
        countDownTimer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(timerFired) userInfo:nil repeats:YES];
        [self.view makeToast:[resultDict objectForKey:@"i"]];
        
    }
    else {
        [self.view makeToast:[resultDict objectForKey:@"i"]];
        
    }
}
- (BOOL)inputShouldNumber:(NSString *)inputString {
    if (inputString.length == 0) return NO;
    NSString *regex =@"[0-9]*";
    NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",regex];
    return [pred evaluateWithObject:inputString];
}



@end
